﻿using Core.Helper;
using Core.Modal;
using System;
using System.Collections.Generic;
using System.Data;

namespace Core.Core
{
    public class ProjectUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public static int Add(string name, string comment) {
           return (int)DbHelper.ExecuteScalar($"exec proc_add_project '{name}','{comment}'");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectid"></param>
        /// <param name="documentid"></param>
        /// <param name="name"></param>
        /// <param name="comment"></param>
        /// <param name="template"></param>
        public static void Update(int projectid,string name, string comment)
        {
            DbHelper.ExecuteNonQuery($"update Project set project_name='{name}',project_comment='{comment}' where project_id='{projectid}'");
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="project_id"></param>
        public static void Delete(int project_id) {
            DbHelper.ExecuteNonQuery($"delete from Project where project_id='{project_id}'");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static modal_Project Get(int id) {            
            DataRow dr=DbHelper.ExecuteDataRow($"select project_id, project_name, project_comment, create_date from Project where project_id='{id}'");
            if (dr != null) {
                modal_Project obj = new modal_Project();
                obj.create_date = Convert.ToDateTime(dr["create_date"].ToString());
                obj.project_id = Convert.ToInt32(dr["project_id"].ToString());
                obj.project_name = dr["project_name"].ToString();
                obj.project_comment = dr["project_comment"].ToString();
                return obj;
            }
            return null;
        }

        public static IList<modal_Project> GetList(string where, string orderby, int pageindex, int pagesize, ref int recordcount) {
            return DbHelper.PageData(orderby, pagesize, pageindex, "Project", where, ref recordcount).ToList<modal_Project>();
        }
    }
}
